package com.kingsoft.miot.bridge.service.impl;

import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kingsoft.miot.bridge.dao.SceneInfoDao;
import com.kingsoft.miot.bridge.exception.CommonException;
import com.kingsoft.miot.bridge.pojo.scene.SceneInfo;
import com.kingsoft.miot.bridge.service.SceneInfoService;

/**
 * 场景服务
 *
 * @author melody
 */
@Service
public class SceneInfoServiceImpl implements SceneInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SceneInfoServiceImpl.class);

    @Autowired
    private SceneInfoDao seneInfoDao;

    /**
     * 新增一条数据
     */
    @Override
    public int insertOne(JSONObject params) {
        SceneInfo sceneInfo = new SceneInfo();
        int id = params.getInteger("id");
        String name = params.get("name").toString();
        JSONObject status = params.getJSONObject("status");
        int value = status.getInteger("value");
        JSONArray info = params.getJSONArray("info");
        sceneInfo.setHotel_info_id(1);
        sceneInfo.setHouse_info_id(1);
        sceneInfo.setScene_info_id(id);
        sceneInfo.setScene_info_name(name);
        sceneInfo.setScene_info_status(value);
        sceneInfo.setScene_info_opration_list(info.toString());
        int count = seneInfoDao.insertOne(sceneInfo);
        return count;
    }

    /**
     * 通过酒店id和房间id获取相应的场景列表
     */
    @Override
    public List<SceneInfo> getByHotelIdAndHouseId(int hotel_info_id, int house_info_id) {
        try {
            HashMap<String, Object> argMap = new HashMap<>();
            argMap.put("hotel_info_id", hotel_info_id);
            argMap.put("house_info_id", house_info_id);
            return seneInfoDao.getByHotelIdAndHouseId(argMap);
        } catch (Exception e) {
            throw new CommonException(e);
        }
    }

}
